<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * User form.
 *
 * @package    form
 * @subpackage users
 * @version    SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class UserForm extends BaseUserForm {
  public function configure() {
  	$this->widgetSchema['password'] = new sfWidgetFormInputPassword();
	$this->widgetSchema['confirm_password'] = new sfWidgetFormInputPassword();
  	
  	$this->validatorSchema['email'] = new sfValidatorEmail(array('required' => true), 
		  array('invalid' => 'The email address is invalid', 'required' => 'An email address is required'));
	
  	$this->validatorSchema['username'] = new sfValidatorString(array('required' => true), 
	  array('invalid' => 'The username is invalid', 'required' => 'A username is required'));
	
	$this->validatorSchema->setPostValidator(
	  new sfValidatorPropelUnique(
	  	array('model' => 'User', 'column' => array('username')), array('invalid' => 'The username is already taken')
	  )
	);
	
	$this->mergePostValidator(
	  new sfValidatorPropelUnique(
	  	array('model' => 'User', 'column' => array('email')), array('invalid' => 'The email address is already registered')
	  )
	);
	
	$this->validatorSchema['password'] = new sfValidatorString(array('max_length' => 40, 'required' => true),
	  array('required' => 'Please enter a password'));
	$this->validatorSchema['confirm_password'] = new sfValidatorString(array('max_length' => 40, 'required' => true),
	  array('required' => 'Please confirm your password'));
	
	// do not validate the md5_password since this will be hashed later
	$this->validatorSchema['md5_password'] = new sfValidatorPass();
	
	// make sure password fields match
	$this->mergePostValidator(
	  new sfValidatorSchemaCompare('password', '==', 'confirm_password', array(),
	    array('invalid' => 'The passwords do not match')
	  )
	);
	
	// possible school networks
	$this->widgetSchema['network_id'] = new sfWidgetFormSelect(
	  array('choices' => UserPeer::$networks)
	);
	
	// field for 'Other' network
	$this->widgetSchema['network_name'] = new sfWidgetFormInput();
	
	// gender
	$gender = array('Male' => 'Male', 'Female' => 'Female');
	$this->widgetSchema['gender'] = new sfWidgetFormSelect(
	  array('choices' => $gender)
	);
	
	//timezone
	$zones=array('1'=>'GMT +1','2'=>"GMT +2",'3'=>"GMT +3",'4'=>"GMT +4",'5'=>"GMT +5",'6'=>"GMT +6",'7'=>"GMT +7",'8'=>"GMT +8",'9'=>"GMT +9", '10' => "GMT +10", '11' => "GMT +11", '12' => "GMT +/-12",'-1'=>'GMT -1','-2'=>"GMT -2",'-3'=>"GMT -3",'-4'=>"GMT -4",'-5'=>"GMT -5",'-6'=>"GMT -6",'-7'=>"GMT -7",'-8'=>"GMT -8",'-9'=>"GMT -9", '-10' => "GMT -10", '-11' => "GMT -11");
	$this->widgetSchema['timezone']= new sfWidgetFormSelect(array('choices' => $zones));
	
	// create a listing of countries
	$this->widgetSchema['country'] = new sfWidgetFormI18nSelectCountry(array('culture' => 'en'));
	$this->setDefault('country', 'US');
	
	$this->widgetSchema['language'] = new sfWidgetFormI18nSelectLanguage(array('culture' => 'en'));
	$this->setDefault('language', 'en');
	
	// receive email updates
	$this->widgetSchema['receive_email_updates'] = new sfWidgetFormInputCheckbox(array('value_attribute_value' => 1));
	$this->setDefault('receive_email_updates', false);
	
	// users must agree to the terms of service
	$this->widgetSchema['tos_agreement'] = new sfWidgetFormInputCheckbox(array('value_attribute_value' => 1));
	$this->validatorSchema['tos_agreement'] = new sfValidatorBoolean(
		array('true_values' => array(1), 'false_values' => array(0), 'required' => true),
		array('required' => 'You must agree to the terms of service', 
		'invalid' => 'You must agree to the terms of service'));
	$this->setDefault('tos_agreement', false);
		
	// users must agree to copyright policy
	$this->widgetSchema['copyright_agreement'] = new sfWidgetFormInputHidden();
	$this->validatorSchema['copyright_agreement'] = new sfValidatorPass();
	$this->setDefault('copyright_agreement', false);
	
	
	// birthdate
	$birth_years = array();
	for($i=2008; $i >= 1920; $i--) {
		$birth_years[$i] = $i;
	}
	$this->widgetSchema['birthdate'] = new sfWidgetFormDate(
		array('format' => '%year%', 'years' => $birth_years, 'can_be_empty' => false,
			'empty_values' => array('month' => 1, 'day' => 1, 'year' => 2008) ));	
	$this->validatorSchema['birthdate'] = new sfValidatorDate(
		array('required' => false, 'with_time' => false), array('invalid' => 'Please select your birth year')
	);
	$this->setDefault('birthdate', time());
	
	$this->widgetSchema['avatar'] = new sfWidgetFormInputFile();
	$this->validatorSchema['avatar'] = new sfValidatorFile(array(
	  'required'   => false,
	  'path'       => 'files/users'
	));
			
	
    $this->validatorSchema->setOption('allow_extra_fields', true);
	$this->validatorSchema->setOption('filter_extra_fields', false);
  }
  
  public function saveAvatar($fileValues) {
  	$user = $this->getObject();		// retrieve the user object
  	
  	// delete the old avatar
  	if(file_exists($user->getAvatar())) {
    	unlink($user->getAvatar());
    }
    
    // delete the avatar thumbnail
    if(file_exists($user->getAvatar('thumb'))) {
    	unlink($user->getAvatar('thumb'));
    }
    
	$user_id = $user->getId();
	$realpath = dirname(__FILE__).'/../../web';  
	$appendPath = 'userfiles/'.Utils::getDigitPath($user_id);
    $file = $this->validatorSchema['avatar']->clean($fileValues);
	$filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
	$file->save($realpath.'/'.$appendPath.'/'.$filename);  // we save the file on filesystem
//	$avatar = $file->getSavedName();	
	$avatar = '/'.$appendPath.'/'.$filename;  // and we set the avatar ref
	$user->setAvatar($avatar);
	
	// retrieve the thumbnail file path by setting and getting it via the user object
//	
//	$thumbnail = $user->getAvatar('thumb');
	
	// create the thumbnail
//	$crop = Utils::crop_thumbnail($avatar, $thumbnail, 30, 30);
//	chmod($thumbnail, 0755);
	
	// resize the original image
//	Utils::scale_image($avatar, $avatar, 250, 250);
//	chmod($avatar, 0755);
	
    return $file;
  }

}
